package com.xxx.system.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.system.domain.Menu;
import com.xxx.system.query.MenuQuery;
import com.xxx.system.service.Impl.MenuServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单管理类接口")
public class MenuController {

    @Autowired
    private MenuServiceImpl menuService;

    @GetMapping
    @ApiOperation(value = "查询所有菜单")
    public R<List<Menu>> findAll() {
        List<Menu> menus = menuService.findAll();
        return R.ok(menus);
    }
    @GetMapping("{/id}")
    @ApiOperation(value = "查询单个菜单")
    public Menu findOne(Long id) {
        return menuService.findOne(id);
    }

    @PostMapping
    @ApiOperation(value = "分页+条件查询")
    public PageInfo<Menu> queryPage(@RequestBody MenuQuery menuQuery) {
        List<Menu> list = menuService.queryPage(menuQuery);
        return new PageInfo<Menu>(list);
    }

    @PutMapping
    @ApiOperation(value = "增加or修改")
    public R<Void> addMenuOrUpdateMenu(@RequestBody Menu menu) {
        try {
            Long id = menu.getId();
            if(id != null){
                menuService.updateMenu(menu);
            }else{
                menuService.addMenu(menu);
            }
        } catch(Exception e) {
            return R.fail();
        }
        return R.ok();
    }



    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个")
    public R<Void> deleteMenuById(@PathVariable Long id) {
        try {
            menuService.deleteMenuById(id);
        } catch(Exception e) {
            return R.fail();
        }
        return R.ok();
    }

    @DeleteMapping
    @ApiOperation(value = "删除多个")
    public R<Void> deleteMenuByIds(Long[] ids) {
        try {
            menuService.deleteMenuByIds(ids);
        } catch (Exception e) {
            return R.fail();
        }
        return R.ok();
    }
    @GetMapping("/menuTree/{id}")
    @ApiOperation(value = "查询菜单树")
    public List<Menu> getMenuTree(){
        List<Menu> menus = menuService.getMenuTree();
        System.out.println(menus);
        return menus;
    }
}
